package br.com.bancodados.repositorio;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import br.com.bancodados.exception.BusinessException;
import br.com.bancodados.model.Carro;
import br.com.bancodados.model.CarrosBaseHelper;
import br.com.bancodados.ui.R;
import br.com.bancodados.util.SQLiteHelper;

public class RepositorioCarroSQlite implements IRepositorioCarro{
	
	private static RepositorioCarroSQlite instance;
	private static Context mContext;
	private static final String NOME_BANCO = "carros_database";
	public static final String NOME_TABELA = "carro";
	private static final int DATABASE_VERSION = 1;
	protected SQLiteDatabase db;
	private SQLiteHelper dbHelper;
	
	
	private RepositorioCarroSQlite(Context context){
		mContext = context;
		openDatabase();
	}
	
	private void openDatabase() {
		try {
			closeDatabase();
				
			dbHelper = new SQLiteHelper(mContext, NOME_BANCO, DATABASE_VERSION,
					CarrosBaseHelper.SCRIPT_DATABASE_CREATE, CarrosBaseHelper.SCRIPT_DATABASE_DELETE);
			db = dbHelper.getWritableDatabase();
		} catch (BusinessException e) {
			e.printStackTrace();
		} 
		
	}

	public void closeDatabase() throws BusinessException{
		if (db != null) {
			try {
				db.close();
			} catch (SQLException sqe) {
				sqe.printStackTrace();
				throw new BusinessException(mContext.getResources().getString(
					R.string.db_error));
			}
		}
	}
	
	public static RepositorioCarroSQlite getInstance(Context context) {
		
		if (instance == null) {
			instance = new RepositorioCarroSQlite(context);
		}
		
		return instance;
	}
	
	

	@Override
	public void atualizarCarro(Carro carro) throws BusinessException {
		ContentValues values = new ContentValues();
		values.put(CarrosBaseHelper.MARCA, carro.getMarca());
		values.put(CarrosBaseHelper.MODELO, carro.getModelo());
		values.put(CarrosBaseHelper.CHASSI, carro.getChassi());
		values.put(CarrosBaseHelper.PLACA, carro.getPlaca());
		values.put(CarrosBaseHelper.COR, carro.getCor());

		String _id = String.valueOf(carro.getCodigo());

		String where = CarrosBaseHelper._ID + "=?";
		String[] whereArgs = new String[] { _id };

		try {
			db.update(NOME_TABELA, values, where, whereArgs);
		} catch (SQLException sqe) {
			sqe.printStackTrace();
			throw new BusinessException(mContext.getResources().getString(
					R.string.db_error));
		}
	}

	@Override
	public void cadastrarCarro(Carro carro) throws BusinessException {
		ContentValues values = new ContentValues();
		values.put(CarrosBaseHelper.MARCA, carro.getMarca());
		values.put(CarrosBaseHelper.MODELO, carro.getModelo());
		values.put(CarrosBaseHelper.CHASSI, carro.getChassi());
		values.put(CarrosBaseHelper.PLACA, carro.getPlaca());
		values.put(CarrosBaseHelper.COR, carro.getCor());
	

		try {
			db.insert(NOME_TABELA, "", values);
			
		} catch (SQLException sqe) {
			sqe.printStackTrace();
			throw new BusinessException(mContext.getResources().getString(
					R.string.db_error_insert_record));
		}
	}

	@Override
	public void deletarCarro(Carro carro) throws BusinessException {
		String where = CarrosBaseHelper._ID + "=?";
		String _id = String.valueOf(carro.getCodigo());
		String[] whereArgs = new String[] { _id };

		try {
			db.delete(NOME_TABELA, where, whereArgs);
		} catch (SQLException sqe) {
			sqe.printStackTrace();
			throw new BusinessException(mContext.getResources().getString(
					R.string.db_error));
		}
	}

	@Override
	public ArrayList<Carro> getAllCarros() throws BusinessException {
		Cursor cursor = null;
		
		ArrayList<Carro> carros;
		try {
			carros = new ArrayList<Carro>();
			cursor = db.query(NOME_TABELA, CarrosBaseHelper.getCarroColunas(), null , null,
					null, null, null, null);
			if (cursor.moveToFirst()) {
				int codigo = cursor.getColumnIndex(CarrosBaseHelper._ID);
				int marca = cursor.getColumnIndex(CarrosBaseHelper.MARCA);
				int modelo = cursor.getColumnIndex(CarrosBaseHelper.MODELO);
				int chassi = cursor.getColumnIndex(CarrosBaseHelper.CHASSI);
				int placa = cursor.getColumnIndex(CarrosBaseHelper.PLACA);
				int cor = cursor.getColumnIndex(CarrosBaseHelper.COR);
				
				do {
					Carro carro = new Carro();
					carro.setCodigo(cursor.getInt(codigo));
					carro.setMarca(cursor.getString(marca));
					carro.setModelo(cursor.getString(modelo));
					carro.setChassi(cursor.getString(chassi));
					carro.setPlaca(cursor.getString(placa));
					carro.setCor(cursor.getString(cor));
				
					carros.add(carro);
				} while (cursor.moveToNext());
				
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new BusinessException(mContext.getResources().getString(
					R.string.db_error));
		} finally {
			if (cursor != null) {
				cursor.close();
			}
		}
		return carros;
	}

	@Override
	public ArrayList<Carro> getCarroPorModelo(String modelo)
			throws BusinessException {
		Cursor cursor = null;
		
		ArrayList<Carro> carros;
		try {
			carros = new ArrayList<Carro>();
			cursor = db.query(NOME_TABELA, CarrosBaseHelper.getCarroColunas(), CarrosBaseHelper.MODELO + "=" + modelo , null,
					null, null, null, null);
			
			if (cursor.moveToFirst()) {
				int codigo = cursor.getColumnIndex(CarrosBaseHelper._ID);
				int marca = cursor.getColumnIndex(CarrosBaseHelper.MARCA);
				int modeloCarro = cursor.getColumnIndex(CarrosBaseHelper.MODELO);
				int chassi = cursor.getColumnIndex(CarrosBaseHelper.CHASSI);
				int placa = cursor.getColumnIndex(CarrosBaseHelper.PLACA);
				int cor = cursor.getColumnIndex(CarrosBaseHelper.COR);
				
				do {
					Carro carro = new Carro();
					carro.setCodigo(cursor.getInt(codigo));
					carro.setMarca(cursor.getString(marca));
					carro.setModelo(cursor.getString(modeloCarro));
					carro.setChassi(cursor.getString(chassi));
					carro.setPlaca(cursor.getString(placa));
					carro.setCor(cursor.getString(cor));
				
					carros.add(carro);
				} while (cursor.moveToNext());
				
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new BusinessException(mContext.getResources().getString(
					R.string.db_error));
		} finally {
			if (cursor != null) {
				cursor.close();
			}
		}
		return carros;
	}


}
